/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmListadoDePagos.java
 *
 * Created on 26/10/2011, 02:07:58 PM
 */
package apiformularios.form;

import api.dom.Factura;
import api.dom.Pago;
import api.dom.Utilitaria;
import api.gest.GestFactura;
import api.gest.GestPago;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author MAT
 */
public class FrmListadoDePagos extends javax.swing.JInternalFrame {

    /** Creates new form FrmListadoDePagos */
    public FrmListadoDePagos() {
        initComponents();
        ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).setNorthPane(null);
        jTable2.setModel(Utilitaria.listarPagos((DefaultTableModel) jTable2.getModel()));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        txtNombre1 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        btnAgregar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        fechahasta = new com.toedter.calendar.JDateChooser();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        fechadesde = new com.toedter.calendar.JDateChooser();
        btnEliminar1 = new javax.swing.JButton();

        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel2.setBackground(new java.awt.Color(153, 180, 209));
        jPanel2.setPreferredSize(new java.awt.Dimension(580, 71));
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel6.setBackground(new java.awt.Color(51, 255, 51));
        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 48));
        jLabel6.setText("Listado de Pagos");
        jLabel6.setToolTipText("");
        jPanel2.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 0, -1, -1));

        getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 650, 70));

        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        txtNombre1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtNombre1KeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtNombre1KeyTyped(evt);
            }
        });
        jPanel3.add(txtNombre1, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 10, 100, -1));

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel8.setText("Buscar por numero: ");
        jPanel3.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, -1));

        getContentPane().add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 520, 250, 40));

        btnAgregar.setText("Modificar");
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });
        getContentPane().add(btnAgregar, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 520, 80, 40));

        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });
        getContentPane().add(btnEliminar, new org.netbeans.lib.awtextra.AbsoluteConstraints(560, 520, 80, 40));

        jTable2.setAutoCreateRowSorter(true);
        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nro", "Fecha Pago", "Nro Factura", "Importe pago", "Saldo factura"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.Object.class, java.lang.Object.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane2.setViewportView(jTable2);

        getContentPane().add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 160, 630, 350));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel3.setText(" *");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(620, 80, -1, -1));

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel7.setText("*");
        getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 80, -1, 20));

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel9.setText("Desde Fecha: ");
        getContentPane().add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 80, -1, -1));

        jPanel1.setBackground(new java.awt.Color(153, 180, 209));

        jLabel10.setBackground(new java.awt.Color(51, 255, 51));
        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 48));
        jLabel10.setText("Listado de Facturas");
        jLabel10.setToolTipText("");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel10)
                .addContainerGap(230, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 650, 70));
        getContentPane().add(fechahasta, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 80, 170, -1));

        jLabel11.setFont(new java.awt.Font("Tahoma", 2, 12));
        jLabel11.setText("Campos Obligatorios (*)");
        getContentPane().add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(500, 130, 140, -1));

        jLabel12.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel12.setText("Hasta Fecha: ");
        getContentPane().add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 80, -1, -1));
        getContentPane().add(fechadesde, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 80, 170, -1));

        btnEliminar1.setText("Listar");
        btnEliminar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminar1ActionPerformed(evt);
            }
        });
        getContentPane().add(btnEliminar1, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 110, 80, 40));

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void txtNombre1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNombre1KeyReleased
    // TODO add your handling code here:
    try {

        if ("".equals(this.txtNombre1.getText())) {
            this.jTable2.setModel(Utilitaria.listarPagos((DefaultTableModel) jTable2.getModel()));
        } else {
            DefaultTableModel modelo32 = (DefaultTableModel) jTable2.getModel();
            modelo32.setRowCount(0);
            Pago objP = GestPago.getInstance().devolverPago(Integer.parseInt(txtNombre1.getText()));
            Object[] objPa = {objP.getpNumeroPago(), objP.getpFechaPago(), objP.getpObjFactura().getfNumero(), objP.getpObjFactura().getfImporte()};
            modelo32.addRow(objPa);
        }
    } catch (NumberFormatException e) {
    } catch (NullPointerException ex) {
        JOptionPane.showMessageDialog(null, "No exsiste tal pago");
        txtNombre1.setText("");
        this.jTable2.setModel(Utilitaria.listarPagos((DefaultTableModel) jTable2.getModel()));
    }

}//GEN-LAST:event_txtNombre1KeyReleased

private void txtNombre1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNombre1KeyTyped
    // TODO add your handling code here:
    char caracter = evt.getKeyChar();
    if (((caracter < '0')
            || (caracter > '9'))
            && (caracter != evt.VK_BACK_SPACE)) {
        evt.consume();
    }

}//GEN-LAST:event_txtNombre1KeyTyped

private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
    try{
    int numPago = Integer.parseInt(String.valueOf(jTable2.getValueAt(jTable2.getSelectedRow(), 0)));
    String fecha = String.valueOf(jTable2.getValueAt(jTable2.getSelectedRow(), 1));
    int numf = Integer.parseInt(String.valueOf(jTable2.getValueAt(jTable2.getSelectedRow(), 2)));
    Factura objF = GestFactura.getInstance().devolverFactura(WIDTH);
    double importe = Double.parseDouble(String.valueOf(jTable2.getValueAt(jTable2.getSelectedRow(), 3)));
    int saldo = Integer.parseInt(String.valueOf(jTable2.getValueAt(jTable2.getSelectedRow(), 4)));
    Pago objP = new Pago(numPago, fecha, objF, importe);
    GestPago.getInstance().modificarPago(objP);
    }catch(IndexOutOfBoundsException ex){
        JOptionPane.showMessageDialog(rootPane, "Seleccione primero un pago");
    }catch(NullPointerException e){
        JOptionPane.showMessageDialog(rootPane, "Seleccione primero un pago");
    }
}//GEN-LAST:event_btnAgregarActionPerformed

private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
    // TODO add your handling code here:
    // TODO add your handling code here:
    try {
        int numeroP = Integer.parseInt(String.valueOf(jTable2.getValueAt(jTable2.getSelectedRow(), 0)));
        Pago objP = GestPago.getInstance().devolverPago(numeroP);
        GestPago.getInstance().bajaPago(numeroP);
        GestFactura.getInstance().modificarSaldoFactura2(objP.getpObjFactura().getfNumero(), (int) objP.getpImportePago());
        jTable2.setModel(Utilitaria.listarPagos((DefaultTableModel) jTable2.getModel()));
    } catch (IndexOutOfBoundsException ex) {
        JOptionPane.showMessageDialog(rootPane, "Seleccione primero un pago");
    } catch (NullPointerException e) {
        JOptionPane.showMessageDialog(rootPane, "Seleccione primero un pago");
    }
}//GEN-LAST:event_btnEliminarActionPerformed

private void btnEliminar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminar1ActionPerformed
    // TODO add your handling code here:
    try{
    Date desde = fechadesde.getDate();
    Date hasta = fechahasta.getDate();
    jTable2.setModel(Utilitaria.listarPagosentreFechas((DefaultTableModel)jTable2.getModel(), desde, hasta));
    }catch(NullPointerException e){
        JOptionPane.showMessageDialog(rootPane, "Seleccione las fechas entre las cuales se filtrara");
        jTable2.setModel(Utilitaria.listarPagos((DefaultTableModel) jTable2.getModel()));
    }
}//GEN-LAST:event_btnEliminar1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnEliminar1;
    private com.toedter.calendar.JDateChooser fechadesde;
    private com.toedter.calendar.JDateChooser fechahasta;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField txtNombre1;
    // End of variables declaration//GEN-END:variables
}
